var app = getApp();
Page({
  data: {
    page: 1,
    no_more: false,
    keyword:'',
    section: [{
      name: '会员列表',
      typeId: '1'
    }, {
        name: '储值会员列表',
      typeId: '2'
    }, {
      name: '红包会员列表',
    typeId: '3'
  }
    ],
    currentId: '1',
  },
  
  handleTap: function (e) {
    var id = e.currentTarget.id;
    this.setData({
      currentId: id,
      page: 1,
      no_more: false,
      keyword:'',
    })
    if(id == 1 || id == 2){
      this.getEnterTheStoreList();
    }else{
      this.getHongBaoStoreList();
    }
  },
  // onLoad: function (options) {
  //   var store_id = wx.getStorageSync("store_id");
  //   var account_id = wx.getStorageSync("account_id");
  //   console.log(store_id)
  //   if(!store_id || !account_id){
  //     app.showToast("请登录小程序")
  //     wx.navigateBack();
  //   }else{
  //     this.setData({
  //       store_id:wx.getStorageSync("store_id"),
  //       account_id:wx.getStorageSync("account_id")
  //     })
  //     this.getEnterTheStoreList();
  //   }
  // },

  onShow:function(){
    var store_id = wx.getStorageSync("store_id");
    var account_id = wx.getStorageSync("account_id");
    console.log(store_id)
    if(!store_id || !account_id){
      app.showToast("请登录小程序")
      wx.navigateBack();
    }else{
      this.setData({
        store_id:wx.getStorageSync("store_id"),
        account_id:wx.getStorageSync("account_id"),
        page: 1,
        no_more: false,
      })
      this.getEnterTheStoreList();
    }
  },

  setKeyword:function(e){
    var keyword = e.detail.value;
    this.setData({
      keyword:keyword,
      page: 1,
      no_more: false
    })
    var currentId = this.data.currentId;
    if(currentId == 1 || currentId == 2){
      this.getEnterTheStoreList();
    }else{
      this.getHongBaoStoreList();
    }
  },

  getEnterTheStoreList:function(){
    var t = this;
    var page = t.data.page;
    var currentId = this.data.currentId;
    app.visit("account/getEnterTheStoreList", {
      page:page,
      store_id:t.data.store_id,
      currentId:t.data.currentId,
      account_id:t.data.account_id,
      keyword: t.data.keyword
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        if (c.data.length < 10) {
          t.setData({
            no_more: true
          })
        }
        if (page == 1) {
          t.setData({
            details: c.data.list,
            balance: c.data.balance,
            znum: c.data.znum,
            vcznum: c.data.vcznum,
            page: page + 1
          })
        } else {
          var details = t.data.details.concat(c.data.list)
          t.setData({
            details: details,
            page: page + 1
          })
        }
      }else{
        app.showToast(c.msg)
      }
    })
  },

  getHongBaoStoreList:function(){
    var t = this;
    var page = t.data.page;
    app.visit("account/getHongBaoStoreList", {
      page:page,
      store_id:t.data.store_id,
      account_id:t.data.account_id,
      keyword: t.data.keyword
    }, "", function(c) {
      console.log(c)
      if(0 == c.code){
        if (c.data.length < 10) {
          t.setData({
            no_more: true
          })
        }
        if (page == 1) {
          t.setData({
            details: c.data.list,
            page: page + 1
          })
        } else {
          var details = t.data.details.concat(c.data.list)
          t.setData({
            details: details,
            page: page + 1
          })
        }
      }else{
        app.showToast(c.msg)
      }
    })
  },

  /**
   * 下拉到底加载数据
   */
  onReachBottom: function () {
    var currentId = this.data.currentId;
    if(currentId == 1 || currentId == 2){
      // 已经是最后一页
      if (this.data.no_more == true) {
        return false;
      } else {
        this.getEnterTheStoreList();
      }
    }else{
      // 已经是最后一页
      if (this.data.no_more == true) {
        return false;
      } else {
        this.getHongBaoStoreList();
      }
    }
  },
  
  hy_info: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'hy_info?id='+id
    });
  },
})